<script setup lang="ts">
// import { useRoute } from 'vue-router';
import { onMounted } from 'vue';

import { useAccessStore } from '@vben/stores';

import { loginShop } from '#/api';
import { indlyfor1688todystor } from '#/store';

import BodyContent from './bodycontent/index.vue';

// const auto1688 = auto1688Store();
// const buyLink = import.meta.env.VITE_APP_BUY_DOUDIAN_LINK;
// const buy1688Link = import.meta.env.VITE_APP_1688_ISV_URL;
const enventss = new Map();
const indlyfor1688tody = indlyfor1688todystor();
function addLententFuns(id: any, cb: any) {
  if (enventss.get(id)) {
    window.removeEventListener('message', enventss.get(id));
    enventss.delete(id);
  }
  enventss.set(id, cb);
  window.addEventListener('message', enventss.get(id));
}
function removeLententFuns(id: any) {
  window.removeEventListener('message', enventss.get(id));
  enventss.delete(id);
}

// const images: any = ref([]);
// const shopData = ref({});
const accessStore = useAccessStore();
const msgFuncs: any = {
  init: async (data: any) => {
    // console.log('init', data);
    // images.value = data.data.images;
    indlyfor1688tody.setImages(data.data.images);

    // shopData.value = data.data.shopData;
    indlyfor1688tody.setShopData(data.data.shopData);
    indlyfor1688tody.loading = true;
    const res = await loginShop(data.data.shopData).catch(() => {
      indlyfor1688tody.loading = false;
    });
    accessStore.setAccessToken(res.access_token);
    accessStore.setRefreshToken(res.refresh_token);
    indlyfor1688tody.getShopInfo();
  },
  clear: () => {
    indlyfor1688tody.$reset();
  },
};
// function toBuyAppMsg() {
//   Modal.confirm({
//     title: '提示',
//     content: '使用当前功能需先授权后使用',
//     okText: '去授权',
//     cancelText: '取消',
//     onOk(clear) {
//       window.open(buyLink, '_blank');
//       clear && clear();
//       userVerify();
//       // window.parent.postMessage({ from: 'dly-1688', type: 'bind' }, '*');
//     },
//   });
// }


// function userVerify() {
//   Modal.confirm({
//     title: '提示',
//     content: '是否成功授权应用',
//     okText: '已授权',
//     cancelText: '未授权',
//     onOk(clear) {
//       clear && clear();
//       getShopInfo();
//     },
//   });
// }

// function getShopInfo() {
//   getShopBindInfo()
//     .then((ress) => {
//       indlyfor1688tody.loading = false;
//       auto1688.set1688BindShop(ress);
//       const getDlyApp = ress.mapList['2'];
//       if (!getDlyApp || getDlyApp.length === 0) {
//         toBuyAppMsg();
//         return;
//       }
//       // toBuyAppMsg();
//       // toBuy1688App();
//       const getusedlyapp = getDlyApp.find(
//         (item: any) =>
//           item.shopId === indlyfor1688tody.shopData.shopId && item.status === 1,
//       ); //  item.isUseing === 1 &&
//       // console.log('getusedlyapp', getusedlyapp);
//       if (!getusedlyapp) {
//         toBuyAppMsg();
//         return;
//       }
//       // if (getusedlyapp && getusedlyapp.status === 0) {
//       //   toBuyAppMsg();
//       //   return;
//       // }
//       indlyfor1688tody.setBuyStatus({
//         buy1688: false,
//         buydly: true,
//       });
//       const get1688App = ress.mapList['1'];
//       // console.log('get1688App', get1688App);
//       if (!get1688App || get1688App.length === 0) {
//         toBuy1688App();
//         return;
//       }
//       // console.log('get1688App', get1688App);
//       const getused1688app = get1688App.find((item: any) => item.status === 1); //  item.isUseing === 1 &&
//       // console.log('getused1688app00215646', getused1688app);
//       if (!getused1688app) {
//         toBuy1688App();
//         return;
//       }

//       // if (getused1688app && getused1688app.status === 0) {
//       //   toBuy1688App();
//       //   return;
//       // }
//       indlyfor1688tody.setBuyStatus({
//         buy1688: true,
//         buydly: true,
//       });
//       indlyfor1688tody.getImageForGoods();
//     })
//     .catch(() => {
//       indlyfor1688tody.loading = false;
//     });
// }

onMounted(() => {
  window.parent.postMessage({ from: 'dly-1688', type: 'init' }, '*');
  sendmessages();
});
// 通信交互
function sendmessages() {
  removeLententFuns('form-doudian-dly-msg-q');
  addLententFuns('form-doudian-dly-msg-q', (e: any) => {
    if (e.data && e.data.from && e.data.from === 'dly-plugin') {
      msgFuncs[e.data.type] && msgFuncs[e.data.type](e.data);
    }
  });
}
// function clearBox() {
//   window.parent.postMessage({ from: 'dly-1688', type: 'clear' }, '*');
// }
// function reloadWin() {
//   window.location.reload();
// }
// function getShopInfssso() {
//   const url = `${buy1688Link}?shopId=${indlyfor1688tody.shopData.shopId}&shopName=${indlyfor1688tody.shopData.shopName}`;
//   open(url);
// }
</script>

<template>
  <div class="h-screen overflow-hidden">
    <!-- <a-button type="primary" @click="getShopInfssso">123456</a-button> -->
    <BodyContent />
    <set-add-product-box
      v-if="indlyfor1688tody.showAddBox"
      v-model="indlyfor1688tody.showAddBox"
      :select-item-list="indlyfor1688tody.selectProductsInOpen"
    />
    <!-- {{ images }}
    {{ shopData }}
    <a-button type="primary" @click="getShopInfo">获取店铺信息</a-button>
    <a-image
      v-if="images && images.length > 0"
      :src="images[0].url"
      :width="60"
    /> -->
    <!-- <a-button type="primary" @click="reloadWin"> 刷新页面 </a-button>
    <a-button type="primary" @click="clearBox"> 关闭 </a-button> -->
  </div>
</template>
